package com.ying.tree;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

// 定义节点类
class TreeNode {
    String name;
    int sortOrder; // 用于排序的顺序
    List<TreeNode> children;

    public TreeNode(String name, int sortOrder) {
        this.name = name;
        this.sortOrder = sortOrder;
        this.children = new ArrayList<>();
    }

    public void addChild(TreeNode child) {
        children.add(child);
    }
}

public class TreePrinter {

    // 打印树的方法
    public static void printTree(TreeNode root) {
        if (root == null) {
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        // 初始化计数器，从100开始
        AtomicInteger counter = new AtomicInteger(100);

        while (!queue.isEmpty()) {
            int levelSize = queue.size();
            List<TreeNode> currentLevelNodes = new ArrayList<>();

            // 收集当前层的所有节点
            for (int i = 0; i < levelSize; i++) {
                TreeNode currentNode = queue.poll();
                currentLevelNodes.add(currentNode);
            }

            // 对当前层的节点按 sortOrder 排序
            Collections.sort(currentLevelNodes, Comparator.comparingInt(n -> n.sortOrder));

            // 打印当前层的所有节点，并使用计数器
            StringBuilder levelOutput = new StringBuilder();
            for (TreeNode node : currentLevelNodes) {
                int currentCount = counter.getAndIncrement();
                levelOutput.append("Node: ").append(currentCount)
                           .append(", Name: ").append(node.name)
                           .append(" (Sort: ").append(node.sortOrder).append(")\n");
            }
            // 打印当前层的结果
            System.out.println(levelOutput.toString().trim());
            System.out.println(); // 添加空行分隔层级

            // 将下一层的节点加入队列
            for (TreeNode node : currentLevelNodes) {
                queue.addAll(node.children);
            }
        }
    }

    public static void main(String[] args) {
        // 创建根节点
        TreeNode root = new TreeNode("Root", 0);

        // 创建第一层级节点
        TreeNode deptA = new TreeNode("Department A", 1);
        TreeNode deptB = new TreeNode("Department B", 2);

        // 创建第二层级节点
        TreeNode subDeptA1 = new TreeNode("Sub-Department A1", 1);
        TreeNode subDeptA2 = new TreeNode("Sub-Department A2", 2);
        TreeNode subDeptB1 = new TreeNode("Sub-Department B1", 1);
        TreeNode subDeptB2 = new TreeNode("Sub-Department B2", 3);

        // 创建第三层级节点
        TreeNode subSubDeptA1a = new TreeNode("Sub-Sub-Department A1a", 4);
        TreeNode subSubDeptA1b = new TreeNode("Sub-Sub-Department A1b", 2);

        TreeNode subSubDeptB1a = new TreeNode("Sub-Sub-Department B1a", 1);

        // 构建树结构
        root.addChild(deptA);
        root.addChild(deptB);
        deptA.addChild(subDeptA1);
        deptA.addChild(subDeptA2);
        deptB.addChild(subDeptB1);
        deptB.addChild(subDeptB2);

        subDeptA1.addChild(subSubDeptA1a);
        subDeptA1.addChild(subSubDeptA1b);
        subDeptB1.addChild(subSubDeptB1a);

        // 打印树
        printTree(root);
    }
}